package com.kedacom.freedomLabel.view.fragment.setting;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;

import com.kedacom.freedomLabel.R;
import com.seray.scaleviewlib.utils.LogUtils;
import com.seray.scaleviewlib.utils.ToastUtils;
import com.tscale.scalelib.jniscale.JNIScale;


public class ScalesSettingFragment extends PreferenceFragment {

    private static JNIScale mScale;

    public ScalesSettingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try{
            initJNI();
            addPreferencesFromResource(R.xml.scale_setting);
//            bindPreferenceSummaryToValue(findPreference("scales_main_unit_full"));
//            bindPreferenceSummaryToValue(findPreference("scales_unit"));
            bindPreferenceSummaryToValue(findPreference("scales_range_mode"));
            bindPreferenceSummaryToValue(findPreference("scales_decimal_point"));
            bindPreferenceSummaryToValue(findPreference("scales_fdntable_1"));
            bindPreferenceSummaryToValue(findPreference("scales_fdntable_2"));
            bindPreferenceSummaryToValue(findPreference("scales_division_values"));
            bindPreferenceSummaryToValue(findPreference("scales_zero_mode"));
        }catch (Exception e){
            LogUtils.d("scaleSettingonCreate===="+e.toString());
            e.printStackTrace();
        }

    }

    private void initJNI() {
        mScale = JNIScale.getScale();
//        mScale.setMainUnitDeci(0);
//        mScale.setUnit(0);
//        mScale.setMainUnitDeci(3);
//        mScale.setStabDelay(150);//秤的稳定时间150  --> 0.75s
    }

    private static void bindPreferenceSummaryToValue(Preference preference) {
        try{
            preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
            sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                    PreferenceManager.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));

        }catch (Exception e){
            LogUtils.d("scaleSetting===="+e.toString());
            e.printStackTrace();
        }
     }

    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {



            String stringValue = value.toString();

            if (preference instanceof ListPreference) {

                ListPreference listPreference = (ListPreference) preference;
                String StringKey = listPreference.getKey();
                int index = listPreference.findIndexOfValue(stringValue);
                preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
                int valueInt = Integer.valueOf(stringValue);

                if (StringKey.equals("scales_unit")) {
                    try{
                        boolean isValueInt =  mScale.setUnit(valueInt);
                        LogUtils.d("isValueInt ===="+isValueInt);
                    }catch (Exception e){
                        LogUtils.d("isValueInt error ===="+e.toString());
                    }

                } else if (StringKey.equals("scales_range_mode")) {
                    mScale.setRangeMode(valueInt);
                } else if (StringKey.equals("scales_decimal_point")) {
                    mScale.setMainUnitDeci(valueInt);
                } else if (StringKey.equals("scales_fdntable_1")) {
                    mScale.setFdnPtr(valueInt);
//                    mScale.setBigFdnPtr(valueInt);
                }
                else if (StringKey.equals("scales_fdntable_2")) {
                    mScale.setBigFdnPtr(valueInt);
                }
                else if (StringKey.equals("scales_division_values")) {
                    mScale.setZeroTrackPtr(valueInt);
                } else if (StringKey.equals("scales_zero_mode")) {
                    mScale.setManualZeroRangePtr(valueInt);


                    // 增加设置自动归零范围
                    mScale.setAutoZeroRangePtr(valueInt);
                }
            } else if (preference instanceof EditTextPreference) {
                int valueInt = 20;
                try {
                    valueInt = Integer.valueOf(stringValue);
                } catch (Exception e) {
                    valueInt = 20;
                }

                if (valueInt > 0) {
                    setManualUnit(valueInt);
                    preference.setSummary(stringValue);
                } else {
                    ToastUtils.showShort(R.string.oper_input_number);
                }


            } else {

                preference.setSummary(stringValue);
            }
            return true;
        }
    };


    public static void setManualUnit(int value) {
            if (value > 0) {
            mScale.setMainUnitFull(value);// 设置主单位称重量程
            float midUnit = value / 2;
            mScale.setMainUnitMidFull(midUnit);// 设置主单位半量程
            float curFullUnit = mScale.getMainUnitFull();
//            if (curFullUnit == value) {// 获取设置最大量程操作后的最大量程，可行
//                ToastUtils.showShort(R.string.oper_max_value_success);
//            } else {
//                ToastUtils.showShort(R.string.oper_max_value_failed);
//            }
        }
    }

    public static void setUnit(int value) {
        mScale.setUnit(value);
    }
}
